package cn.com.fig.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.com.fig.common.PaginationReqParam;
import cn.com.fig.common.PaginationResponse;
import cn.com.fig.common.ReplyMsg;
import cn.com.fig.service.PrivilegeService;
import cn.com.fig.service.TradeSysService;

/**
 * 菜单维护
 *
 */
@RestController
@RequestMapping("/privilege")
public class PrivilegeControl {
	
	@Autowired
	private PrivilegeService privilegeService;
	@Autowired
	private TradeSysService tradeSysService;
	
	/**
	 * 
	 * @param level 0表示初始化菜单树查询；1表示查询业务系统下下级菜单；2表示查询某菜单下级菜单;3表示没有下级菜单
	 * @param id
	 * @return
	 */
	@RequestMapping("/privilegeTree")
	public List<Map<String,Object>> tree(int level,String id) {
		List<Map<String,Object>> ll = new ArrayList<Map<String,Object>>();
		PaginationReqParam reqParam = new PaginationReqParam ();
		reqParam.setNeedSplitPage(false);		
		
//		初始化菜单树
		if(level == 0){
			List<Map<String,Object>> sysList = tradeSysService.listTradeSys(reqParam).getRows();
			Map<String,Object> tree = new HashMap<String,Object>();
			tree.put("id","00");
			tree.put("text","菜单树");
			List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
			for(int i=0; i< sysList.size(); i++){
				Map<String,Object> t = new HashMap<String,Object>();
				t.put("id",(String)sysList.get(i).get("sysId"));
				t.put("text",(String)sysList.get(i).get("sysName"));
				t.put("state","closed");
				Map<String,Object> attributes = new HashMap<String, Object>();
				attributes.put("systemId", true);
				t.put("attributes", attributes);
				list.add(t);
			}
			tree.put("children", list);
			ll.add(tree);
		}
//		查询业务系统下所有菜单
		if(level == 1){
			Map<String,String> privilegeMap = new HashMap<String,String>();
			privilegeMap.put("top", "1");
			privilegeMap.put("systemId", id);			
			List<Map<String,Object>> priList = privilegeService.listPrivilege(reqParam, privilegeMap).getRows();
			for(int i=0; i< priList.size(); i++){
				Map<String,Object> t = new HashMap<String,Object>();
				t.put("id",(String)priList.get(i).get("privillegeId"));
				t.put("text",(String)priList.get(i).get("privilName"));
				Map<String,Object> attributes = new HashMap<String, Object>();
				attributes.put("privillegeId", true);
				t.put("attributes", attributes);
				List<Map<String,Object>> childrenList = getChildren((String)priList.get(i).get("privillegeId"));
				if(childrenList != null){
					t.put("state","closed");
					t.put("children", childrenList);
				}
				ll.add(t);
			}
		}
		
//		查询某菜单下级所有菜单
//		if(level == 2){
//			List<Map<String,Object>> priList = getChildren(id);
//			if(priList != null)
//				ll.addAll(priList);
//		}
		return ll;
	}
	/**
	 * 递归方式查询一级菜单下所有子菜单
	 * @param parentId
	 * @return
	 */
	private List<Map<String,Object>> getChildren(String parentId){
		List<Map<String,Object>> ll = null;
		PaginationReqParam reqParam = new PaginationReqParam ();
		reqParam.setNeedSplitPage(false);
		Map<String,String> privilegeMap = new HashMap<String,String>();
		privilegeMap.put("parentId", parentId);
		List<Map<String,Object>> priList = privilegeService.listPrivilege(reqParam, privilegeMap).getRows();
		if(priList.size() > 0)
			ll = new ArrayList<Map<String,Object>>();
		for(int i=0; i< priList.size(); i++){
			Map<String,Object> t = new HashMap<String,Object>();
			t.put("id",(String)priList.get(i).get("privillegeId"));
			t.put("text",(String)priList.get(i).get("privilName"));
			Map<String,Object> attributes = new HashMap<String, Object>();
			attributes.put("leaf", true);
			t.put("attributes", attributes);
			List<Map<String,Object>> cc = getChildren((String)priList.get(i).get("privillegeId"));
			if(cc != null){
				t.put("state","closed");
				t.put("children", cc);
			}
			ll.add(t);
		}
		return ll;
	}
	
	@RequestMapping("/list")
	public PaginationResponse<Map<String,Object>> list(PaginationReqParam reqParam,@RequestParam Map<String,String> privilegeMap) {
		return privilegeService.listPrivilege(reqParam, privilegeMap);
	}
	
	@RequestMapping("/save")
	public ReplyMsg save(@RequestParam Map<String,Object> privilegeMap){
		return privilegeService.save(privilegeMap);
	}
	
	@RequestMapping("/del")
	public ReplyMsg del(String privillegeId){
		return privilegeService.del(privillegeId);
	}

}
